package Service_Impl;

import java.util.List;

import javax.servlet.http.Cookie;

import Dao.IStudentDao;
import Dao_Impl.StudentDaoImpl;
import Entity.PageBeanStudent;
import JavaServlet.TimeServlet.Student;
import Service.IStudentService;

public class StudentServiceImpl implements IStudentService{
	private IStudentDao studentDao = new StudentDaoImpl();
	
	@Override
	public List<Student> selectAll() {
		// 体现Service业务逻辑，给所有班级名字前面加上"刘老师"
		List<Student>list = studentDao.selectAll();
		for(Student student : list) {
			String name = student.getName();
			student.setName("Java1907--" + name);
		}
		return list;
	}
	
	@Override
	public boolean insert(Student student) {
		// 1、数据库中查看是不是有该姓名的学生 selectByName(name)
		// 2.1、如果有：表示该名字用户已经存在,不去调用insert
		// 2.2、如果没有：表示该名字用户不存在,去调用insert插入这个用户
		List<Student> list =  studentDao.selectByName(student.getName());
		if(list.size() > 0) {
			System.out.println("该用户已存在");
			return false;
		}else {
			int count = studentDao.insert(student);
			return count == 1 ? true : false;
		}
	}

	@Override
	public boolean update(Student student) {
		int count = studentDao.update(student);
		return count == 1 ? true : false;
	}
	
	@Override
	public boolean deleteById(Integer id) {
		// 业务逻辑：id在10-100之间的不删除
		int count = studentDao.deleteById(id);
		return count == 1 ? true : false;
	}
	
	@Override
	public List<Student> searchName(String name){
		return studentDao.searchName(name);
	}
	
	@Override
	public PageBeanStudent pageList(Integer pageNo, Integer pageSize){
		PageBeanStudent pageBeanStudent = new PageBeanStudent();
		// 当前是第几页
		pageBeanStudent.setPageNo(pageNo);
		// 一页有多少条数据
		pageBeanStudent.setPageSize(pageSize);
		// 一共有多少页
		int count = studentDao.getTotalCount();
		int totalPage = (int)Math.ceil((double)count / pageSize);
		pageBeanStudent.setTotalPage(totalPage);
		
		// 当前页数据
		int offset = (pageNo - 1) * pageSize;
		List<Student>list = studentDao.pageList(offset,pageSize);
		pageBeanStudent.setList(list);
		return pageBeanStudent;
	}

	@Override
	public Student selectById(int id) {
		return studentDao.selectById(id);
	}

	@Override
	public boolean checkName(String checkName) {
		List<Student>list = studentDao.selectByName(checkName);
		return list.size() > 0 ? true : false;
	}
}
